package org.umlviewer.model.sequence_diagram;

import java.awt.*;
import java.awt.geom.Point2D;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.umlviewer.model.Constants;
import org.umlviewer.model.DiagramElement;
import org.umlviewer.paint.PaintUtils;

/**
 *
 * @author Rafa
 */
public class LifeLine extends DiagramElement{
    
    private int width = 80, height=40, bodyLen = 100;
    
    public LifeLine()
    {
        setName("");
    }
    
    public LifeLine(String name)
    {
        setName(name);
    }   
    
    @Override
    public void paint(Graphics g2) {
        
        Graphics2D g = (Graphics2D) g2;
        g.setColor(getLineColor());
        g.drawRect(getPosX(), getPosY(), width, height);
        g.setColor(getFillColor());
        g.fillRect(getPosX()+1, getPosY()+1, width-1, height-1);
        g.setColor(getLineColor());
        
        Stroke originalStroke = g.getStroke();
        Stroke dotted = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] {10,5}, 0);
        //nombre
        g.setColor(Constants.DEFAULTCOLOR);
        g.setFont(Constants.boldFont);
        PaintUtils.drawCenterString(g, getName(), getPosX(), getPosY()+15, width);
        //g.drawString(getName(), getPosX() + 5, getPosY() + 15);
        g.setFont(Constants.defaultFont);
        g.setStroke(dotted);
        g.setColor(getLineColor());
        g.drawLine(getPosX() + width/2, getPosY() + height, getPosX() + width/2, getPosY() + height + bodyLen );
        g.setStroke(originalStroke);
        setWidth(width);
        setHeight(height + bodyLen);
        g.setColor(Color.black);
        
        if(isSelected())
            super.paintSelected(g);
    }
    
    /***
     * Se sobrescribe para permitir solo desplazamientos horizontales
     * @param x
     * @param y 
     */
    public void move(int x, int y){
        setPosX(x);
        setPosY(y);
    }

    public void resize(Rectangle r){
        setPosX(r.x);
        this.width = r.width;        
        this.bodyLen = r.height - this.height;
    }
    
    public Point2D getRelPoint(Point away){
        return new Point(getPosX() + width/2,getPosY()+height);
    }
    
    public Element xmiSerialize(){
        Namespace xmins = Constants.XMINAMESPACE;
        Element lf = new Element("lifeline");
        lf.setAttribute("type","uml:Lifeline",xmins);
        lf.setAttribute("id",getId(),xmins);
        lf.setAttribute("name",getName());
        lf.setAttribute("visibility","public");
        return lf;
    }
}
